package cn.edu.bzu.manager.computer;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * MVC配置类
 *
 * @author 1923111015董寺卓
 * @since 1.8
 */

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    /**
     * 使用注册器添加拦截器
     *
     * @param registry 自动注入的注册器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new HandlerInterceptor() {
            // 访问指定规则前验证
            @Override
            public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
                if (request.getSession().getAttribute("userId") != null) return true;
                ComputerComponentManager.LOGGER.warn("未登录");
                return false;
            }
        }).addPathPatterns("/component/**", "/cpu/**", "/gpu/**", "/deliver/**");
    }

}
